package it.datagraf.formazione.hellostruts.dao;

import it.datagraf.formazione.hellostruts.entity.CurriculumVitae;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class CurriculumVitaeDaoHbm implements CurriculumVitaeDaoI {

	private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
	@Override
	public List<CurriculumVitae> list() {
		Session s=sessionFactory.getCurrentSession();
		return s.createQuery("from CurriculumVitae").list();
	}
	
	@Override
	public int saveOrUpdate(CurriculumVitae cv) {
		this.sessionFactory.getCurrentSession().saveOrUpdate(cv);
		return cv.getId().intValue();
	}

	public CurriculumVitae find(CurriculumVitae cv)  {
		Session s=sessionFactory.getCurrentSession();
		Long cv_id=cv.getId();
		CurriculumVitae result= (CurriculumVitae)s.createQuery("from CurriculumVitae where id= :cv_id").setParameter("cv_id", cv_id).uniqueResult();
		return result;
	}
	
	public CurriculumVitae find(int id)
	{
		Long id2del = new Long(id);
		return (CurriculumVitae) this.sessionFactory.getCurrentSession().createQuery("from CurriculumVitae where id= :cv_id").setParameter("cv_id", id2del).uniqueResult();
	}
	
	
	public int findId(String owner){
		CurriculumVitae cv =(CurriculumVitae) this.sessionFactory.getCurrentSession().createQuery("from CurriculumVitae where owner= :own").setParameter("own", owner).uniqueResult();
		return cv.getId().intValue();
	}
	
	public void remove(int id){
		this.sessionFactory.getCurrentSession().createQuery("delete CurriculumVitae where id= :cv_id").setParameter("cv_id", new Long(id)).executeUpdate();
	}
	
	
}